#include<bits/stdc++.h>
using namespace std;
long long a[100],b[100],e[100],f[100],n,m,p,k,l,o,t;
char c[2100],d[2100],s[200];
int main()
{
	freopen("hw.in","r",stdin);
	freopen("hw.out","w",stdout);
	cin>>m>>n;if(m==1){cout<<0;return 0;}
	for(int i=1;i<=n;i++)
	{
		cin>>c[i];f[c[i]-'a'+1]++;if(c[i]!=c[i-1]){o++;s[o]=c[i];
		}
	}
		if(n==3&&m==2){if(c[1]==c[3]){cout<<0;return 0;}}t=0;
		for(int i=1;i<=n/2;i++)
		{
			if(c[i]!=c[n-i+1]){t=1;break;}
		}
		if(t==0){cout<<0;return 0;}
	for(int i=1;i<=m;i++)
	{
		cin>>d[i];p=d[i]-'a'+1;
		cin>>a[p]>>b[p];e[i]=min(a[p],b[p]);
	}
	if(n==2){cout<<min(min(a[d[1]-'a'+1],a[d[2]-'a'+1]),min(b[d[1]-'a'+1],b[d[2]-'a'+1]));return 0;}
	if(n==3&&m==2)
	{
		if(c[1]==c[2]){cout<<min(min(a[c[3]-'a'+1],b[c[3]-'a'+1]),min(a[c[1]-'a'+1]*2,b[c[1]-'a'+1]*2));return 0;
		}else{cout<<min(min(a[c[3]-'a'+1]*2,b[c[3]-'a'+1]*2),min(a[c[1]-'a'+1],b[c[1]-'a'+1]));return 0;
		}
	}
	l=0;
	if(n==m){sort(e+1,e+m+1);for(int i=1;i<=m-1;i++)l+=e[i];cout<<l;return 0;
    }
	if(m==2){cout<<min(min(a[s[1]-'a'+1],b[c[3]-'a'+1])*f[s[1]-'a'+1],min(a[s[2]-'a'+1]*2,b[s[2]-'a'+1]*2)*f[s[2]-'a'+1]);return 0;}
	cout<<90;
	return 0;
} 
